package com.kl.controller;


import com.alibaba.fastjson.JSONObject;
import com.kl.domain.TbTaxpayerDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.TbContacts;
import com.kl.entity.TbTaxpayer;
import com.kl.entity.UserScore;
import com.kl.service.ITbTaxpayerService;
import com.kl.service.IUserScoreService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@RestController
@RequestMapping("tbTaxpayerController")
public class TbTaxpayerController {

    @Autowired
    private ITbTaxpayerService tbTaxpayerService;

    //纳税人信息查询接口
    @ApiOperation(value = "纳税人信息查询接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "userName",value = "用户名称",required = true,paramType = "query")
    })
    @GetMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result<List<TbTaxpayerDTO>> selectByTaxpayerId(@RequestParam String userName){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        List<TbTaxpayer> contactsList = null;
        try {
            //pageList = userMapper.selectPage(page, null);
            contactsList = tbTaxpayerService.selectByUserName(userName);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        String string = JSONObject.toJSONString(contactsList);
        List<TbTaxpayerDTO> tbTaxpayerDTOS = JSONObject.parseArray(string, TbTaxpayerDTO.class);
        return Result.build(ConstantEnum.SUCCESS).setResult(tbTaxpayerDTOS);
    }

    //纳税人信息增加接口
    @ApiOperation(value = "纳税人信息增加接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "tbTaxpayerDTO",value = "纳税人信息实体类",required = true,paramType = "body")
    })
    @PostMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result inserInto(@RequestBody TbTaxpayerDTO tbTaxpayerDTO){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        try {
            TbTaxpayer tbTaxpayer = new TbTaxpayer();
            BeanUtils.copyProperties(tbTaxpayerDTO,tbTaxpayer);
            tbTaxpayerService.save(tbTaxpayer);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    //纳税人信息修改接口
    @ApiOperation(value = "纳税人信息修改接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "tbTaxpayerDTO",value = "纳税人信息实体类",required = true,paramType = "body")
    })
    @PutMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result updateInfo(@RequestBody TbTaxpayerDTO tbTaxpayerDTO){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        try {
            TbTaxpayer tbTaxpayer = new TbTaxpayer();
            BeanUtils.copyProperties(tbTaxpayerDTO,tbTaxpayer);
            tbTaxpayerService.updateById(tbTaxpayer);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    //纳税人信息删除接口
    @ApiOperation(value = "纳税人信息删除接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "tbId",value = "纳税人ID",required = true,paramType = "query")
    })
    @DeleteMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result deleteByTaxpayerId(@RequestParam Long tbId){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        try {
            tbTaxpayerService.removeById(tbId);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }
}
